package org.geektimes.projects.userweb.hash.consistent2;

import org.geektimes.projects.userweb.hash.IHashService;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * @Author: jipeng
 * @Description:
 * @Date: Created in 2021/5/17 23:03
 */
public class HashServiceImpl2 implements IHashService {

    /**
     *
     *
     * @param key String
     * @return Long
     */
    @Override
    public Long hash(String key) {
        final int p = 16777619;
        long hash = 2166136261L;
        for (int i = 0; i < key.length(); i++) {
            hash = (hash ^ key.charAt(i)) * p;
        }
        hash += hash << 13;
        hash ^= hash >> 7;
        hash += hash << 3;
        hash ^= hash >> 17;
        hash += hash << 5;

        // 如果算出来的值为负数则取其绝对值
        if (hash < 0) {
            hash = Math.abs(hash);
        }
        return hash;
    }
}
